const app = getApp();
const util = require('../../../utils/util.js');
const lbbService = require('../../../pages/service/service.js');
const lbbTaskService = require('../service/task-service.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        active:0,
        markShow:false,
        selectMonth:(new Date().getYear()+1900) + '年' +(new Date().getMonth()+1) +'月',
        minDate:new Date(2010, 0, 1).getTime(),
        currentDate:new Date().getTime(),
        // 签到
        activeSign:0,

        // 补签
        activeApplySign:0,
        pageNo:1,
        pageSize:10,

        showRemark:false,
        selected:'',

        dispatchInfo:false,
        formData:'',
        currentTime: '12:00',
        filter(type, options) {
            if (type === 'minute') {
                return options.filter((option) => option % 5 === 0);
            }
            return options;
        },
        timeShow:false,
        onBeforeClose(action, done) {
            if (action == 'confirm') {
                return false
            } else {
                return true
            }
        },
    },
    changeActive(e) {
        this.setData({
            active:e.currentTarget.dataset.key
        })
        if(this.data.active==0) {
            this.setData({
                activeSign:0,
                pageNo:1,
                pageSize:10,
            })
            this.getSignInList()
        } else {
            this.setData({
                activeApplySign:0,
                pageNo:1,
                pageSize:10,
            })
            this.getApplySignInList()
        }
    },
    ondropdown: function() {
        this.setData({
            markShow:true
        })
    },
    formatDate(date) {
        date = new Date(date);
        return `${date.getYear() +1900 }年${date.getMonth() + 1}月`;
    },
    onMarkClose() {
        this.setData({ markShow: false });
    },
    onMarkConfirm(event) {
        this.data.selectMonth = this.formatDate(event.detail)
        this.setData({
          markShow: false,
          selectMonth: this.data.selectMonth,
        });
        this.getSignInList()
    },
    tabChange(e) {
        let that = this
        that.data.pageNo =1
        this.setData({
            activeSign:e.detail.index,
            pageNo:that.data.pageNo
        })
        this.getSignInList()
    },
    tabApplyChange(e) {
        let that = this
        that.data.pageNo =1
        this.setData({
            activeApplySign:e.detail.index,
            pageNo:that.data.pageNo
        })
        this.getApplySignInList()
    },
    //上拉加载更多
    eventhandle(e) {
        let that = this
        let pagenum = Number(that.data.pageNo)
        if (pagenum >= that.data.TotalPage) {
            that.setData({
                loading: false,
                loaded: true
            })
        } else {
            pagenum = pagenum + 1
            that.data.pageNo = pagenum.toString()
            that.setData({
                loading: true,
                loaded: false,
                pageNo:that.data.pageNo
            })
            if(that.data.active==0){
                that.getSignInList()
            } else {
                that.getApplySignInList()
            }
        }
    },
    // 签到列表
    getSignInList() {
        let that = this
        let params = {
            pageNo:that.data.pageNo,
            pageSize:that.data.pageSize,
            signInStatus:that.data.activeSign==0 ? 2 : that.data.activeSign==1 ? 0 : that.data.activeSign==2 ? 1 : 2,
            month:that.data.selectMonth
        }
        wx.showLoading({
            title: '加载中',
        })
        lbbTaskService.getSignStatistics(params,function(res){
            console.log(res.data)
            if(that.data.pageNo==1) {
                let lists = [ ...res.data.records]
                if(lists.length==0) {
                    that.setData({
                        loading: false,
                        loaded: true,
                        TotalPage:res.data.pages,
                        signInList:lists
                    })
                    wx.hideLoading()
                } else {
                    that.setData({
                        loading: false,
                        loaded: true,
                        TotalPage:res.data.pages,
                        signInList:lists
                    })
                    wx.hideLoading()
                  }
              } else {
                  let lists = [...that.data.signInList, ...res.data.records]
                  console.log(lists)
                  if(lists.length==0) {
                      that.setData({
                        loading: false,
                        loaded: true,
                        TotalPage:res.data.pages,
                        signInList:lists
                      })
                    wx.hideLoading()
                  } else {
                      that.setData({
                          loading: false,
                          loaded: true,
                          TotalPage:res.data.pages,
                          signInList:lists
                      })
                      wx.hideLoading()
                  }
              }
              console.log(that.data.signInList)
        })
    },
    // 补签列表
    getApplySignInList() {
        let that = this
        let params = {
            pageNo:that.data.pageNo,
            pageSize:that.data.pageSize,
            applyStatus:that.data.activeApplySign,
        }
        wx.showLoading({
            title: '加载中',
        })
        lbbTaskService.getApplySignList(params,function(res){
            console.log(res.data)
            if(that.data.pageNo==1) {
                let lists = [ ...res.data.records]
                if(lists.length==0) {
                    that.setData({
                        loading: false,
                        loaded: true,
                        TotalPage:res.data.pages,
                        applySignInList:lists
                    })
                    wx.hideLoading()
                } else {
                    that.setData({
                        loading: false,
                        loaded: true,
                        TotalPage:res.data.pages,
                        applySignInList:lists
                    })
                    wx.hideLoading()
                  }
              } else {
                  let lists = [...that.data.applySignInList, ...res.data.records]
                  console.log(lists)
                  if(lists.length==0) {
                      that.setData({
                        loading: false,
                        loaded: true,
                        TotalPage:res.data.pages,
                        applySignInList:lists
                      })
                    wx.hideLoading()
                  } else {
                      that.setData({
                          loading: false,
                          loaded: true,
                          TotalPage:res.data.pages,
                          applySignInList:lists
                      })
                      wx.hideLoading()
                  }
              }
              console.log(that.data.applySignInList)
        })
    },
    viewReason(e) {
        this.setData({
            showRemark:true,
            selected:{
                rejectReason:e.currentTarget.dataset.item.rejectReason
            }
        })
    },
    onConfirmRemark() {
        this.setData({
            showRemark:false,
            selected:''
        })
    },
    applySupply(e) {
        this.setData({
            dispatchInfo:true,
            formData:{
                id:e.currentTarget.dataset.item.id,
                placeType:e.currentTarget.dataset.item.placeType,
                signInDate:e.currentTarget.dataset.item.signInDate,
                signInTime:'12:00'
            }
        })
    },
    selectTime() {
        this.setData({
            timeShow:true
        })
    },
    onTimeClose() {
        this.setData({
            timeShow:false
        })
    },
    onTimeConfirm(event) {
        let that = this
        that.data.formData.signInTime = event.detail
        this.setData({
            timeShow:false,
            formData:that.data.formData
        })
    },
    //校验提示
    openPrompt() {
        wx.showToast({
            title: '请检查必填项',
            icon: 'error',
            duration: 2000
        })
    },
    onCloseDispatch() {
        this.setData({
            dispatchInfo:false,
            formatDate:''
        })
    },
    onConfirmDispatch(){
        let that = this
        if(that.data.formData.signInTime) {
            wx.showLoading({
                title: '加载中',
            })
            lbbTaskService.marketApplySignIn(that.data.formData,function(res){
                wx.hideLoading()
                that.setData({
                    dispatchInfo:false,
                    formatDate:''
                })
                that.getSignInList()
            })
        } else {
            that.openPrompt()
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        if(this.data.active==0) {
            this.getSignInList()
        } else {
            this.getApplySignInList()
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})